﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using WeChat.AspNet.Events;

namespace WeChat.AspNet
{
    /// <summary>
    /// 微信请求对象，对微信发出的xml进行解析处理，转换成C#可处理的对象。
    /// </summary>
    public class WeChatRequest
    {
        private readonly string _requestXml;
        private readonly Hashtable _request; 

        public WeChatRequest(string requestXml)
        {
            _requestXml = requestXml;

            _request = new Hashtable();

        }

        /// <summary>
        /// 解析xml后，对事件进行响应。
        /// </summary>
        /// <param name="resposeObject"></param>
        public void RaiseEvent(IResposeObject resposeObject)
        {
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(_requestXml);
            var bodyNode = xmlDocument.ChildNodes[0];
            if (resposeObject == null)
            {
                return;
            }
            if (bodyNode.ChildNodes.Count > 0)
            {
                foreach (XmlNode xn in bodyNode.ChildNodes)
                {
                    _request.Add(xn.Name, xn.InnerText);
                }

                var msgType = ((string)_request["MsgType"]).ToEnum<ReqMsgType>();

                switch (msgType)
                {
                    case ReqMsgType.Event:

                        //事件处理。
                        ResponseEvents(resposeObject);
                        break;
                    case ReqMsgType.Text:
                        
                        // 接收文本消息。
                        resposeObject.OnTextReceived(this, new TextReceivedEventArgs(_request));
                        break;
                    case ReqMsgType.Image:

                        // 接收图片消息。
                        resposeObject.OnImageReceived(this, new ImageReceivedEventArgs(_request));
                        break;
                    case ReqMsgType.Location:

                        // 接收位置消息。
                        resposeObject.OnLocationReceived(this, new LocationReceivedEventArgs(_request));
                        break;
                    case ReqMsgType.Link:

                        // 接收链接消息。
                        resposeObject.OnLinkReceived(this, new LinkReceivedEventArgs(_request));
                        break;
                    case ReqMsgType.Voice:

                        // 接收语音消息。
                        resposeObject.OnVoiceReceived(this, new VoiceReceivedEventArgs(_request));
                        break;
                    case ReqMsgType.Video:

                        // 接收视频消息。
                        resposeObject.OnVideoReceived(this, new VideoReceivedEventArgs(_request));
                        break;
                }
            }
        }

        /// <summary>
        /// 响应事件。
        /// </summary>
        /// <param name="resposeObject"></param>
        private void ResponseEvents(IResposeObject resposeObject)
        {
            var eventValue = ((string) _request["Event"]).ToEnum<ReqEvents>();

            switch (eventValue)
            {
                case ReqEvents.Subscribe:
                case ReqEvents.Unsubscribe:

                    // 用户关注/取消关注事件。
                    resposeObject.OnSubscribe(this, new SubscribeEventArgs(_request));
                    break;
                case ReqEvents.MenuClick:

                    // 菜单点击事件。
                    resposeObject.OnMenuClicked(this, new MenuClickedEventArgs(_request));
                    break;
                case ReqEvents.Scan:

                    // 扫描二维码事件。 
                    resposeObject.OnScan(this, new ScanEventArgs(_request));
                    break;
                case ReqEvents.ReportLocation:

                    // 上报地理位置。 
                    resposeObject.OnReportLocation(this, new ReportLocationEventArgs(_request));
                    break;
            }
        }
    }
}